package org.gthelper.server;



import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gthelper.error.ErrorCode;
import org.gthelper.handler.Handler;
import org.gthelper.handler.HandlerFactory;
import org.gthelper.order.OrderInfo;
import org.gthelper.security.ValidationRSA;
import org.gthelper.testtools.Printer;


/**
 * GTHelper服务端入口Servlet
 */
@WebServlet("/GTServer")
public class GTServer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GTServer() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		GTServletInitXML.gtInit(config,"/WEB-INF/conf.xml");
	}
    
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doPost(request,response);
		PrintWriter pw = new PrintWriter(response.getWriter());
		pw.write("<html>\r\n<body>");
		pw.write("<h1 align=\"center\">This is Pig's web server</h1>");
		pw.write("<br/><p align=\"center\">The get method is not avaliable, use post instead!</p>");
		pw.write("<br/><p align=\"center\">You can also visit the project page at oschina.net to learn more about our project :</p>");
		pw.write("<p align=\"center\">\r\n<a href=http://git.oschina.net/chenning0115/GTHelper>Go to the project page now!</a>\r\n</p>");
		pw.write("</body>\r\n</html>");
	}
    
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		OrderInfo orderinfo = new OrderInfo();
		orderinfo.setMainStream(request.getInputStream(), response.getOutputStream(), request);
			
		//预处理数据流
		MsgPretreater mrt = new MsgPretreater();
		int mrt_errorcode = mrt.treat(orderinfo, new ValidationRSA());
		
		Printer.print("order in GTServer", "user:\t"+orderinfo.userID + "\t>>>\torder number:\t"+orderinfo.order);
		
		//预处理正确则构造handler类并执行相应指令
		if(mrt_errorcode==ErrorCode.NORMAL)
		{
			//
			Handler handler = HandlerFactory.createHandlerImpl(orderinfo.order);
			if(handler!=null)
			{
				mrt_errorcode=handler.handle(orderinfo);
			}
			else
			{
				mrt_errorcode=ErrorCode.BUILDCLASSERR;
			}	
		}
		
		//有错误
		if(mrt_errorcode!=ErrorCode.NORMAL)
		{
			Printer.print("Error!!!", "errorcode:\t"+mrt_errorcode+"\torder:\t"+orderinfo.order+"\t\t@user:\t"+orderinfo.userID);
			//向客户端返回错误码
			DataOutputStream dout = new DataOutputStream(response.getOutputStream());
			long ec = ErrorCode.ERRBASE + mrt_errorcode;
			dout.writeLong(ec);
			
			dout.flush();
			dout.close();
			
			return ;
		}
		
		
	}
	
}
